package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.service.INursingPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-20
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanServiceImpl nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.copyProperties(nursingPlan, NursingPlanVo.class);
        List<NursingProjectPlan> projectPlans = nursingProjectPlanService.list(new LambdaQueryWrapper<NursingProjectPlan>().
                eq(NursingProjectPlan::getPlanId, nursingPlanVo.getId()));
        nursingPlanVo.setProjectPlans(BeanUtil.copyToList(projectPlans, NursingProjectPlanVo.class));
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划DTO
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.insert(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (projectPlans != null && !projectPlans.isEmpty()) {
            projectPlans.forEach(v -> v.setPlanId(nursingPlan.getId()));
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划Dto
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        nursingProjectPlanService.remove(new LambdaQueryWrapper<NursingProjectPlan>().
                eq(NursingProjectPlan::getPlanId,nursingPlanDto.getId()));
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (projectPlans != null && !projectPlans.isEmpty()) {
            projectPlans.forEach(v -> v.setPlanId(nursingPlanDto.getId()));
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        return nursingPlanMapper.updateById(nursingPlan);
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanById(Long id) {
        nursingProjectPlanService.remove(new LambdaQueryWrapper<NursingProjectPlan>().
                eq(NursingProjectPlan::getPlanId,id));
        return nursingPlanMapper.deleteById(id);
    }

    /**
     * 查询护理计划列表
     *
     * @return 护理计划集合
     */
    @Override
    public List<NursingPlan> selectAll() {
        return nursingPlanMapper.selectList(new LambdaQueryWrapper<NursingPlan>().eq(NursingPlan::getStatus,1));
    }
}
